<?php

/**
 * Description of EncheresManager_PDO
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Encheres\Models;

if( !defined('IN') ) die('Hacking Attempt');

class EncheresManager_PDO extends EncheresManager{
    
    /*
     * Liste toute les encheres
     */
    public function getListe(){
        
        $sql = 'SELECT id, nom, enchere, DATE_FORMAT(dateFin, "%d/%m/%Y %H:%i") as dateFin
                FROM '.$this->nameTable.'
                WHERE termine = "0" AND une = "0"
                ORDER BY id DESC';
		$requete = $this->dao->query($sql);
        
        return $this->fecthAssoc_data($requete, $this->name);
    }
    
    public function getEnchereUne(){
        
        $sql='SELECT e.id, e.nom, e.prix, e.enchere, e.images, e.dateFin, e.reserve, e.limite, e.retime, e.termine, e.pause, e.meneur, e.begin, e.duree, u.pseudo
              FROM '.$this->nameTable.' AS e
              LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
              WHERE e.une = "1" AND termine = "0" AND e.dateFin > "'.date('Y-m-d H:i:s').'" 
              ORDER BY dateFin DESC, e.ordre
              LIMIT 1';
        
        $requete = $this->dao->query($sql);
        
        return $this->fecthAssoc_data($requete, $this->name);
    }
    
    public function nbrEncheresEnCours(){

    }
    
    /*
     * Liste toute les encheres du moment
     */
    public function getEncheres(){
        $sql = 'SELECT b.*
                FROM '.$this->nameTable.' as b
                ORDER BY b.ordre, b.id';
        $obj=$this->dao->query($sql);
        
        return $this->fecthAssoc_data($obj, $this->name);
    }
    
    /*
     * Liste toute les encheres a venir
     */
    public function getEncheresAvenir(){
        $sql='SELECT e.id, e.nom, e.prix, e.enchere, e.images, UNIX_TIMESTAMP(e.dateDebut) as dateDebut, e.reserve, e.limite, e.retime, e.termine, e.pause, e.meneur, e.begin, e.duree, u.pseudo
              FROM '.$this->nameTable.' AS e
              LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
              WHERE e.begin ="0"
              ORDER BY e.ordre, e.id';
        $requete = $this->dao->query($sql);
        
        return $this->fecthAssoc_data($requete, $this->name);
    }
    
    /*
     * Liste toute les encheres terminées
     */
    public function getEncheresTerminees(){
        
        $sql='SELECT e.id, e.nom, e.prix, e.enchere, e.images, e.dateFin, e.reserve, e.limite, e.retime, e.termine, e.pause, e.meneur, e.sent, u.pseudo, u.email
              FROM '.$this->nameTable.' AS e
              LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
              WHERE "'.date('Y-m-d H:i:s').'" >= e.dateFin AND e.begin ="1"
              ORDER BY dateFin DESC, e.ordre';
        $requete = $this->dao->query($sql);
        
        return $this->fecthAssoc_data($requete, $this->name);
    }
    
    /**
     *reccupère une enchere a partir de son Id
     * @param type $ids
     * @return type 
     */
    public function getEnchereById($ids){
        $sql='SELECT e.id, e.nom, e.prix, e.images, e.enchere, e.dateFin, DATE_FORMAT(dateFin, "%Y/%m/%d %H:%i") as fin, e.duree, e.reserve, e.limite, e.retime, e.ae, e.robots, e.description, e.pause, e.meneur, e.termine, e.sent, u.pseudo
              FROM '.$this->nameTable.' AS e
              LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
              WHERE e.id = :id';
        $detail = $this->dao->prepare($sql);
		$detail->bindParam(':id', intval($ids));
		$detail->execute();
		
        return $this->fecthAssoc_data($detail, $this->name);
    }
    
    /**
     *met à jour la table enrichisseur
     * @param array $param 
     */
    public function enrichisseurs(array $param){
        $sql = 'INSERT INTO c2w_encherisseurs SET user_id=:user, product_id=:product, dateEnchere =NOW()';
        $rqte = $this->dao->prepare($sql);
        $rqte->bindParam(':user', intval($param['iduser']));
        $rqte->bindParam(':product', intval($param['idprod']));
        $rqte->execute()?$this->encherirEnchere($param):'';
    }
    
    /**
     * met à jour un enchère suite d'un encherissement
     * @param array $param 
     */
    public function encherirEnchere(array $param){
        $sql = 'UPDATE '.$this->nameTable.' AS e
                SET meneur=:meneur, enchere =enchere +0.01, dateFin = :dateFin
                WHERE e.id=:id';
        $rqte = $this->dao->prepare($sql);
        $rqte->bindParam(':meneur', intval($param['iduser']));
        $rqte->bindValue(':dateFin', $param['addtime']);
        $rqte->bindParam(':id', intval($param['idprod']));
        return $rqte->execute();
        
    }
    
    public function getEnrichisseurs($ids){
        $liste = $this->dao->prepare('SELECT DATE_FORMAT(e.dateEnchere, "%d/%m") AS mois, DATE_FORMAT(e.dateEnchere, "%H:%i:%s") as heure, u.pseudo
		 FROM c2w_encherisseurs AS e
		 INNER JOIN c2w_utilisateurs AS u ON e.user_id = u.id
		 WHERE e.product_id = :id
		 ORDER BY e.dateEnchere DESC
		 LIMIT 5');
		
		$liste->bindParam(':id', intval($ids));
		$liste->execute();
        $output = array();
        foreach( $liste->fetchAll(\PDO::FETCH_ASSOC) as $result ) {
		
			$output[] = $result;
	
		}
        return $output;
    }
    
    public function addEnchere(array $param){
        $objData = new Encheres($param);
        
        $sql='INSERT INTO '.$this->nameTable.' (nom, prix, images, dateFin, duree, reserve, limite, description, retime, ae, robots, une, ordre, dateDebut) VALUES (:nom, :prix, :images, :dateFin, :duree, :reserve, :limite, :description, :retime, :autoEnchere, :robots, :une, :ordre, :dateDebut)';
        
        $req=$this->dao->prepare($sql);
        $req->bindParam(':nom',$objData->getNom());
        $req->bindParam(':prix',$objData->getPrix());
        $req->bindParam(':images',$objData->getImages());
        $req->bindParam(':duree',$objData->getDuree());
        $req->bindParam(':dateFin',$objData->getDateFin());
        $req->bindParam(':reserve',$objData->getReserve());
        $req->bindParam(':limite',$objData->getLimite());
        $req->bindParam(':retime',$objData->getRetime());
        $req->bindParam(':autoEnchere',intval($objData->getAutoEnchere()));
        $req->bindParam(':robots',intval($objData->getRobots()));
        $req->bindParam(':une',intval($objData->getUne()));
        $req->bindParam(':ordre',intval($objData->getOrdre()));
        $req->bindParam(':description',$objData->getDescription());
        $req->bindParam(':dateDebut',$objData->getDateDebut());
        
        return $req->execute();
    }
    
    /**
     * supprime une enchère
     * @param array $id
     * @return type 
     */
    public function deleteEncheres(array $id){
        
        return $this->delete($this->nameTable, $id);
    }
    /**
     * Mise àjour d'une enchère
     * @param array $param
     * @return type 
     */
    public function updateEnchere(array $param) {
        $objData = new Encheres($param);
        
        $sql='UPDATE '.$this->nameTable.' SET nom = :nom, prix = :prix, enchere = 0, images = :images, dateFin = :dateFin, duree = :duree, reserve = :reserve, limite = :limite, retime = :retime, ae = :autoEnchere, robots = :robots, description = :description, dateDebut = :dateDebut, une = :une, ordre = :ordre, begin = 0, sent = 0, meneur ="" WHERE id = :id';

        $req=$this->dao->prepare($sql);
        $req->bindParam(':nom',$objData->getNom());
        $req->bindParam(':prix',$objData->getPrix());
        $req->bindParam(':images',$objData->getImages());
        $req->bindParam(':duree',$objData->getDuree());
        $req->bindParam(':dateFin',$objData->getDateFin());
        $req->bindParam(':reserve',$objData->getReserve());
        $req->bindParam(':limite',$objData->getLimite());
        $req->bindParam(':retime',$objData->getRetime());
        $req->bindParam(':autoEnchere',intval($objData->getAutoEnchere()));
        $req->bindParam(':robots',intval($objData->getRobots()));
        $req->bindParam(':une',intval($objData->getUne()));
        $req->bindParam(':ordre',intval($objData->getOrdre()));
        $req->bindParam(':description',$objData->getDescription());
        $req->bindParam(':dateDebut',$objData->getDateDebut());
        $req->bindParam(':id',$objData->getId());
        
        return $req->execute();
    }
    
    /**
     * reccupère les enchères en cours pour la home page
     * @return type 
     */
    public function getEnchereEnCours(){
        $sql = 'SELECT e.id, e.nom, e.prix, e.enchere, e.images, e.dateFin, e.reserve, e.limite, e.retime, e.termine, e.pause, e.meneur, e.begin, e.duree, u.pseudo
                FROM '.$this->nameTable.' AS e
                LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
                WHERE e.dateFin > "'.date('Y-m-d H:i:s').'" AND e.une = "0" AND e.termine = "0"
                ORDER BY e.id, e.ordre
                LIMIT ' . _ENCHERES_EN_COURS_;
        $obj=$this->dao->query($sql);
        
        return $this->fecthAssoc_data($obj, $this->name);
    }
    
    /**
     * reccupère les enchères à venir 
     * @return type 
     */
    public function getEncheresAvenirFollowers() {
        
        $sql = 'SELECT e.id, e.nom, e.prix, e.enchere, e.images, UNIX_TIMESTAMP(e.dateFin) as dateFin, e.reserve, e.limite, e.retime, e.termine, e.pause, e.meneur, u.pseudo
                FROM '.$this->nameTable.' AS e
                LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
                WHERE e.dateFin > NOW() AND e.une = "0"
                ORDER BY e.id, e.ordre
                LIMIT ' . _ENCHERES_EN_COURS_. ', '._LIMIT_PER_PAGE_;
        $obj=$this->dao->query($sql);
        
        return $this->fecthAssoc_data($obj, $this->name);
		
	}
    function updateBeginEnchere(array $param){
        $objData = new Encheres($param);
        $sql = 'UPDATE '.$this->nameTable.' SET begin = 1, dateFin = :dateFin WHERE id = :id';
        
        $req=$this->dao->prepare($sql);
        $req->bindParam(':dateFin',$objData->getDateFin());
        $req->bindParam(':id',$objData->getId());
        
        return $req->execute();
    }
    
}

?>
